package com.zhou.copyjar;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import static com.zhou.copyjar.ManifestDependencyParser.parseManifestDependencies;

/**
 * @author zhoulj(1217102780 @ qq.com)
 * @version 2.0.0
 * @description:
 * @date 2025/7/17 14:32
 */
public class CopyJar {
    static void copy(String srcPathStr, String desPathStr) {
        //获取源文件的名称
        try {
            FileInputStream fis = new FileInputStream(srcPathStr);//创建输入流对象
            File desFile = new File(desPathStr);
            File parentFile = desFile.getParentFile();
            if (!parentFile.exists()) {
                parentFile.mkdirs();
            }
            desFile.createNewFile();
            FileOutputStream fos = new FileOutputStream(desPathStr); //创建输出流对象
            byte datas[] = new byte[1024 * 1];//创建搬运工具
            int len = 0;//创建长度
            while ((len = fis.read(datas)) != -1)//循环读取数据
            {
                fos.write(datas, 0, len);
            }
            fis.close();//释放资源
            fis.close();//释放资源
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private static List<Path> findJarFiles()  {
        try {
            List<Path> list = Files.list(Paths.get("."))
                    .filter(path ->
                            Files.isRegularFile(path) &&
                                    path.getFileName().toString().toLowerCase().endsWith(".jar")
                    && !path.getFileName().toString().toLowerCase().contains("copyjar")).map(m->m.normalize()).toList();
            return list;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        if (args.length < 1) {
            System.out.println("请输入共享jar包路径");
            return;
        }
        String shareJarPath = args[0];
        File shareJarPathFile = new File(shareJarPath);
        if (!shareJarPathFile.exists()) {
            System.out.println("共享jar包路径不存在");
            return;
        }


        List<Path> jarFiles = findJarFiles();
        if (jarFiles == null || jarFiles.size() == 0) {
            System.out.println("未找到Jar文件");
            return;
        }
        if (jarFiles.size() > 1) {
            System.out.println("找到多个Jar文件");
            return;
        }
        try {
            File jarFile = jarFiles.get(0).toFile();
            File libTargetDir = new File("lib");
            if (!libTargetDir.exists() && !libTargetDir.isDirectory()) {
                libTargetDir.mkdir();
            }
            Set<String> dependencies = parseManifestDependencies(jarFile);

            if (dependencies == null || dependencies.isEmpty()) {
                return;
            }
            for (String dependency : dependencies) {
                if (dependency.startsWith("lib/")) {
                    dependency = dependency.substring(4);
                }
                if (dependency.startsWith("lib\\")) {
                    dependency = dependency.substring(4);
                }
                dependency = dependency.replace(";", "");
                System.out.println( "拷贝文件："+dependency);
                copy(shareJarPath + File.separator + dependency, libTargetDir.getAbsolutePath() +File.separator + dependency);
            }


        } catch (IOException e) {
        }
    }
}
